/**
 * 210824：升级修改【three.js r120-r131】,修改为继承原生库
 */
import * as THREE from 'three';
import {GPUComputationRenderer as GPUComputationRendererOri} from "three/examples/jsm/misc/GPUComputationRenderer";

export default class GPUComputationRenderer extends GPUComputationRendererOri{
    constructor(sizeX,sizeY,renderer,constantType=THREE.FloatType){
        super(sizeX,sizeY,renderer);
        this.setDataType(constantType);
        console.warn('this class is deprecated now, use original class instead!');
    }
}

//20210824
// let THREE=require('three');
//
// export default class GPUComputationRenderer{
//     constructor(sizeX, sizeY, renderer,constantType=THREE.FloatType){
//         this.sizeX=sizeX;
//         this.sizeY=sizeY;
//         this.renderer=renderer;
//         this.constantType=constantType;
//         this.variables = [];
//         this.currentTextureIndex = 0;
//         this.awake();
//     }
//     awake(){
//         this.scene = new THREE.Scene();
//         this.camera = new THREE.Camera();
//         this.camera.position.z = 1;
//         this.passThruUniforms = {
//             texture: { value: null }
//         };
//         this.passThruShader = this.createShaderMaterial( PassThroughFragmentShader, this.passThruUniforms );
//         this.mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), this.passThruShader );
//         this.scene.add( this.mesh );
//     }
//     addVariable( variableName, computeFragmentShader, initialValueTexture ) {
//         let material = this.createShaderMaterial( computeFragmentShader );
//         let variable = {
//             name: variableName,
//             initialValueTexture: initialValueTexture,
//             material: material,
//             dependencies: null,
//             renderTargets: [],
//             wrapS: null,
//             wrapT: null,
//             minFilter: THREE.NearestFilter,
//             magFilter: THREE.NearestFilter
//         };
//         this.variables.push( variable );
//         return variable;
//     }
//     static setVariableDependencies( variable, dependencies ) {
//         variable.dependencies = dependencies;
//     }
//     init() {
//         if ( ! this.renderer.extensions.get( "OES_texture_float" ) ) {
//             console.warn("No OES_texture_float support for float textures.");
//         }
//         if ( this.renderer.capabilities.maxVertexTextures === 0 ) {
//             console.warn("No support for vertex shader textures.");
//         }
//         for ( let i = 0; i < this.variables.length; i++ ) {
//             let variable = this.variables[ i ];
//             variable.renderTargets[ 0 ] = this.createRenderTarget( this.sizeX, this.sizeY, variable.wrapS, variable.wrapT, variable.minFilter, variable.magFilter );
//             variable.renderTargets[ 1 ] = this.createRenderTarget( this.sizeX, this.sizeY, variable.wrapS, variable.wrapT, variable.minFilter, variable.magFilter );
//             this.renderTexture( variable.initialValueTexture, variable.renderTargets[ 0 ] );
//             this.renderTexture( variable.initialValueTexture, variable.renderTargets[ 1 ] );
//             let material = variable.material;
//             let uniforms = material.uniforms;
//             if ( variable.dependencies !== null ) {
//                 for ( let d = 0; d < variable.dependencies.length; d++ ) {
//                     let depVar = variable.dependencies[ d ];
//                     if ( depVar.name !== variable.name ) {
//                         let found = false;
//                         for ( let j = 0; j < this.variables.length; j++ ) {
//                             if ( depVar.name === this.variables[ j ].name ) {
//                                 found = true;
//                                 break;
//                             }
//                         }
//                         if ( ! found ) {
//                             return "Variable dependency not found. Variable=" + variable.name + ", dependency=" + depVar.name;
//                         }
//                     }
//                     uniforms[ depVar.name ] = { value: null };
//                     material.fragmentShader = "\nuniform sampler2D " + depVar.name + ";\n" + material.fragmentShader;
//                 }
//             }
//         }
//         this.currentTextureIndex = 0;
//         return null;
//     }
//     compute() {
//         let currentTextureIndex = this.currentTextureIndex;
//         let nextTextureIndex = this.currentTextureIndex === 0 ? 1 : 0;
//         for ( let i = 0, il = this.variables.length; i < il; i++ ) {
//             let variable = this.variables[ i ];
//             if ( variable.dependencies !== null ) {
//                 let uniforms = variable.material.uniforms;
//                 for ( let d = 0, dl = variable.dependencies.length; d < dl; d++ ) {
//                     let depVar = variable.dependencies[ d ];
//                     uniforms[ depVar.name ].value = depVar.renderTargets[ currentTextureIndex ].texture;
//                 }
//             }
//             this.doRenderTarget( variable.material, variable.renderTargets[ nextTextureIndex ] );
//         }
//         this.currentTextureIndex = nextTextureIndex;
//     }
//     getCurrentRenderTarget( variable ) {
//         return variable.renderTargets[ this.currentTextureIndex ];
//     }
//     getAlternateRenderTarget( variable ) {
//         return variable.renderTargets[ this.currentTextureIndex === 0 ? 1 : 0 ];
//     }
//     addResolutionDefine( materialShader ) {
//         materialShader.defines.resolution = 'vec2( ' + this.sizeX.toFixed( 1 ) + ', ' + this.sizeY.toFixed( 1 ) + ')';
//     }
//     createShaderMaterial( computeFragmentShader, uniforms ) {
//         uniforms = uniforms || {};
//         let material = new THREE.ShaderMaterial( {
//             uniforms: uniforms,
//             vertexShader: PassThroughVertexShader,
//             fragmentShader: computeFragmentShader
//         } );
//         this.addResolutionDefine( material );
//         return material;
//     }
//     createRenderTarget ( sizeXTexture, sizeYTexture, wrapS, wrapT, minFilter, magFilter ) {
//         sizeXTexture = sizeXTexture || this.sizeX;
//         sizeYTexture = sizeYTexture || this.sizeY;
//         wrapS = wrapS || THREE.ClampToEdgeWrapping;
//         wrapT = wrapT || THREE.ClampToEdgeWrapping;
//         minFilter = minFilter || THREE.NearestFilter;
//         magFilter = magFilter || THREE.NearestFilter;
//         return new THREE.WebGLRenderTarget( sizeXTexture, sizeYTexture, {
//             wrapS: wrapS,
//             wrapT: wrapT,
//             minFilter: minFilter,
//             magFilter: magFilter,
//             format: THREE.RGBAFormat,
//             type:this.constantType,
//             stencilBuffer: false,
//             depthBuffer:false,
//         } );
//     }
//     createTexture( sizeXTexture, sizeYTexture ) {
//         sizeXTexture = sizeXTexture || this.sizeX;
//         sizeYTexture = sizeYTexture || this.sizeY;
//         let a=null;
//         if(this.constantType===THREE.FloatType){
//             a = new Float32Array( sizeXTexture * sizeYTexture * 4 );
//         }else if(this.constantType===THREE.UnsignedByteType){
//             a = new Uint8Array(sizeXTexture * sizeYTexture * 4);
//         }
//         let texture = new THREE.DataTexture( a, this.sizeX, this.sizeY, THREE.RGBAFormat,this.constantType);
//         texture.needsUpdate = true;
//         return texture;
//     }
//     renderTexture( input, output ) {
//         this.passThruUniforms.texture.value = input;
//         this.doRenderTarget( this.passThruShader, output);
//         this.passThruUniforms.texture.value = null;
//     }
//     doRenderTarget( material, output ) {
//         this.mesh.material = material;
//         let oriTar=this.renderer.getRenderTarget();
//         this.renderer.setRenderTarget(output);
//         this.renderer.render( this.scene, this.camera);
//         this.renderer.setRenderTarget(oriTar);
//         this.mesh.material = this.passThruShader;
//     }
// }
// let PassThroughFragmentShader=[
//     "uniform sampler2D textureMap;\n" +
//     "\n" +
//     "void main() {\n" +
//     "\n" +
//     "	vec2 uv = gl_FragCoord.xy / resolution.xy;\n" +
//     "\n" +
//     "	gl_FragColor = texture2D( textureMap, uv );\n" +
//     "\n" +
//     "}\n"
// ].join('\n');
// let PassThroughVertexShader=[
//     "void main()	{\n" +
//     "\n" +
//     "	gl_Position = vec4( position, 1.0 );\n" +
//     "\n" +
//     "}\n"
// ].join('\n');
